package org.zjt.date;

import lombok.extern.slf4j.Slf4j;

import java.time.*;
import java.time.temporal.ChronoUnit;

/**
 * Description:
 *
 * @author juntao.zhang
 * Date: 2018-06-04 下午5:06
 * @see
 */
@Slf4j
public class TimeComparer {


    /**
     *  Period   类方法getYears（），getMonths（）和getDays（）来计算.
     * @param args
     */
    public static void periodTest() {

        LocalDate today = LocalDate.now();
        System.out.println("Today : " + today);
        LocalDate birthDate = LocalDate.of(1993, Month.OCTOBER, 19);
        System.out.println("BirthDate : " + birthDate);

        Period p = Period.between(birthDate, today);
        System.out.printf("年龄 : %d 年 %d 月 %d 日", p.getYears(), p.getMonths(), p.getDays());
    }


    /**
     *  Duration 时间的值（如秒，纳秒）测量时间量的方法。
     */
    public static void durationTest() {

        Instant inst1 = Instant.now();
        System.out.println("Inst1 : " + inst1);

        Instant inst2 = inst1.plus(Duration.ofSeconds(10));
        System.out.println("Inst2 : " + inst2);

        System.out.println("Difference in milliseconds : " + Duration.between(inst1, inst2).toMillis());

        System.out.println("Difference in seconds : " + Duration.between(inst1, inst2).getSeconds());

    }


    /**
     * ChronoUnit类  可用于在单个时间单位内测量一段时间，例如天数或秒。
     */
    public static void chronoUnitTest() {
        
        LocalDate startDate = LocalDate.of(1993, Month.OCTOBER, 19);
        System.out.println("开始时间  : " + startDate);

        LocalDate endDate = LocalDate.of(2017, Month.JUNE, 16);
        System.out.println("结束时间 : " + endDate);

        long daysDiff = ChronoUnit.DAYS.between(startDate, endDate);
        System.out.println("两天之间的差在天数   : " + daysDiff);

    }


    public static void main(String[] args) {

        LocalDateTime localDateTime = LocalDateTime.now().plusSeconds(-200);
        System.out.println(Duration.between(localDateTime,LocalDateTime.now()).toMillis());


        log.info(String.valueOf(ChronoUnit.MILLIS.between(localDateTime,LocalDateTime.now())));


        LocalDate localDate = LocalDate.now().plusDays(-23);
        log.info(String.valueOf(Period.between(localDate,LocalDate.now()).getDays()));
    }


}
